<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" style="margin-top: 15px">
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:100px">民宿名称:</label>
        <div class="layui-input-inline" style="width: 8%">
            <input type="text" name="hotelName" id="hotelName" autocomplete="off"
                   class="layui-input">
        </div>
        <label class="layui-form-label">省:</label>
        <div class="layui-input-inline" style="width: 100px">
            <select name="provinceCode" id="provinceCode" lay-filter="province">
            </select>
        </div>
        <label class="layui-form-label" style="width: 30px">市:</label>
        <div class="layui-input-inline" style="width: 100px">
            <select name="cityCode" id="cityCode" lay-filter="city">
            </select>
        </div>
        <label class="layui-form-label" style="width: 30px">区:</label>
        <div class="layui-input-inline" style="width: 100px">
            <select name="districtCode" id="districtCode">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 100px">民宿编号:</label>
        <div class="layui-input-inline" style="width: 8%">
            <input type="text" name="id" id="id" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-input-inline" style="width: 8%">
            <label class="layui-form-label" style="width: 70px">状态:</label>
            <div class="layui-input-block" style="width: 100px">
                <select name="isShow" id="isShow">
                    <option value="">全部</option>
                    <option value="0">下架</option>
                    <option value="1">上架</option>
                </select>
            </div>
        </div>

        <button class="layui-btn" id="searchBtn" lay-submit lay-filter="searchBtn" style="margin-left: 100px">查询
        </button>
        <button class="layui-btn" id="addBtn" lay-submit lay-filter="addBtn">新增民宿</button>
    </div>
</form>
<table class="layui-table" id="grid" lay-filter="grid">
</table>
<script th:replace="/common/inc::incJs"></script>
<script>
    setup();
    var tableIns;

    //变更门店状态
    function changeStatus(hotelId, status) {
        if (status == 0) {
            $.post('/backend/security/hotel/update', {id: hotelId, status: 1}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("上架成功");
                        tableIns.reload();
                    }
                }
            )
        } else if (status = 1) {
            $.post('/backend/security/hotel/update', {id: hotelId, status: 0}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("下架成功");
                        tableIns.reload();
                    }
                }
            )
        }
    }

    //新增按钮
    layui.form.on('submit(addBtn)', function () {
        openDialog({
            url: "/backend/security/hotel/detail",
            area: ["80%", "95%"],
            btn: ""
        });
        return false;
    });

    //查询功能
    layui.form.on('submit(searchBtn)', function (data) {
        tableIns.reload({
            where: data.field
        });
        return false;
    });

    $(function () {
        tableIns = createTable({
            url: '/backend/security/hotel/findHotelList',
            cols: [[{
                field: 'id',
                title: '民宿编号',
                width: 100
            }, {field: 'hotelNickName', title: '民宿别称', width: 180},
                {
                    field: 'hotelAddress',
                    title: '民宿地址',
                    width: 400
                }, {field: 'hotelName', title: '民宿名称', width: 250}, {field: 'mobile', title: '店长号码', width: 120}, {field: 'status', title: '状态', width: 100, templet: '#showTpl'}, {
                    field: 'createTime',
                    title: '创建时间',
                    width: 200
                },
                {
                    fixed: 'right',
                    title: '操作',
                    width: 300,
                    align: 'center',
                    toolbar: '#opbar'
                }
            ]], height: 'full-130'
        });


        //数据表格工具条
        layui.table.on('tool(grid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'change') { //
                layer.confirm('确认变更民宿状态?', function (index) {
                    changeStatus(data.id, data.status);
                });
            } else if (layEvent === 'edit') { //编辑
                openDialog({
                    url: "/backend/security/hotel/detail?id=" + data.id,
                    area: ["80%", "95%"],
                    btn: ""
                });
            } else if (layEvent === 'new') {
                openDialog({
                    url: "/backend/security/hotel/newHotel?id=" + data.id + "&hotelName=" + data.hotelName,
                    area: ["70%", "85%"],
                    btn: ["确认", "取消"]
                });
            } else if (layEvent === 'latestauditstatus') {
                openDialog({
                    url: "/backend/security/hotel/latestauditstatus?chainId=" + data.chainId + "&shopName=" + data.shopName,
                    area: ["80%", "85%"],
                    btn: ""
                });
            }
        });
        layui.form.render('select');
    });

</script>
<script type="text/html" id="showTpl">
    {{#  if(d.status ===0){ }}
    <span style="color: #FF0000;">下架</span>
    {{#  } else { }}
    <span style="color: green;">上架</span>
    {{#  } }}
</script>
<script type="text/html" id="opbar">
    <a class="layui-btn layui-btn-xs opbarEdit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="change">变更状态</a>
</script>
</body>
</html>
